package at.jku.se.fastsell.gui;

import java.util.Timer;
import java.util.TimerTask;

import at.jku.se.fastsell.FastsellUI;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.themes.Reindeer;

public class AllAuctionsView extends MainView implements IRefreshable, IViewStatus {

	private static final long serialVersionUID = 1L;

	public static final String NAME = "auctionsOverview";

	private VerticalLayout mainLayout;
	private HorizontalLayout controlsLayout;
	private Button btnNewAuction;
	private AuctionsTable tblAuctions;
	private Label lblHead;
	private CheckBox chbOnlyWonAuctions;
	
	private TextField txtSearch;
	private Button btnSearch;

	private Timer timer;

	// ----------------------------------------------------------------------------------

	public AllAuctionsView() {
		buildMainLayout();
		this.setContent(mainLayout, this);
	}

	// ----------------------------------------------------------------------------------

	private void buildMainLayout() {
		mainLayout = new VerticalLayout();
		mainLayout.setSpacing(true);

		lblHead = new Label("My overview");
		lblHead.setStyleName(Reindeer.LABEL_H2);
		mainLayout.addComponent(lblHead);
		
		controlsLayout = new HorizontalLayout();
		controlsLayout.setSpacing(true);
		mainLayout.addComponent(controlsLayout);

		btnNewAuction = new Button("New Auction");
		btnNewAuction.addClickListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				FastsellUI.navigateTo(NewAuctionView.NAME);
			}
		});
		controlsLayout.addComponent(btnNewAuction);
		
		chbOnlyWonAuctions = new CheckBox("Show only won auctions");
		chbOnlyWonAuctions.addValueChangeListener(new ValueChangeListener() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				if (chbOnlyWonAuctions.getValue()) {
					tblAuctions.enableFilter(new AuctionsTableFilter(true, getUser()));
				} else {
					tblAuctions.enableFilter(null);
				}
			}
		});
		controlsLayout.addComponent(chbOnlyWonAuctions);
		controlsLayout.setComponentAlignment(chbOnlyWonAuctions, Alignment.MIDDLE_CENTER);
		
		txtSearch = new TextField();
		controlsLayout.addComponent(txtSearch);
		controlsLayout.setComponentAlignment(txtSearch, Alignment.BOTTOM_CENTER);
		
		btnSearch = new Button("Search");
		btnSearch.addClickListener(new ClickListener() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				String search = txtSearch.getValue();
				
				if(search != null) {
					tblAuctions.enableFilter(new AuctionsTableFilter(search));
				}
				
			}
		});
		controlsLayout.addComponent(btnSearch);
		controlsLayout.setComponentAlignment(btnSearch, Alignment.MIDDLE_CENTER);
		

		tblAuctions = new AuctionsTable();
		mainLayout.addComponent(tblAuctions);
	}

	// ----------------------------------------------------------------------------------

	@Override
	public void refreshData() {
		tblAuctions.refreshData();
		
		if (timer == null) {
			timer = new Timer();
			timer.scheduleAtFixedRate(new TimerTask() {

				@Override
				public void run() {
					refreshData();
				}
			}, FastsellUI.POLL_INTERVAL, FastsellUI.POLL_INTERVAL);
		}
	}

	@Override
	public void setInactive() {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
	}

	@Override
	public String getName() {
		return NAME;
	}

}
